﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace SeriesWatcher.DataModel
{
    class SerialManager
    {
        private List<Serial> serials;
        public List<Serial> Serials
        {
            get { return serials; }
            set { serials = value; }
        }

        /// <summary>
        /// Adding serial
        /// </summary>
        /// <param name="name">Serial name</param>
        /// <returns>Added serial</returns>
        public Serial AddSerial(string name)
        {
            Serial serial = new Serial();
            serial.Name = name;
            serials.Add(serial);
            return serial;
        }
        /// <summary>
        /// Default constructor
        /// </summary>
        public SerialManager()
        {
            serials = new List<Serial>();
        }

        public bool SaveToXml(string path)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlElement element = doc.CreateElement("serial");
                doc.AppendChild(element);

                foreach (Serial serial in serials)
                {
                    element.AppendChild(serial.SaveToXml(doc));
                }

                doc.Save(path);
            }
            catch(XmlException xmlEx)
            {
                MessageBox.Show(xmlEx.Message);
            }
            catch(Exception ex)         // Handle the generic exceptions here.
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            
            }

            
            return true;
        }

    }
}
